package top.infopub.mgr.mkt.service.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.DeciProAttDao;
import top.infopub.mgr.mkt.dao.DeciProCoopDao;
import top.infopub.mgr.mkt.dao.DeciProDao;
import top.infopub.mgr.mkt.dao.DeciProQuoteDao;
import top.infopub.mgr.mkt.dao.DeciProSubDao;
import top.infopub.mgr.mkt.domain.DeciProDomain;
import top.infopub.mgr.mkt.service.DeciProService;
import top.infopub.mgr.mkt.vo.DeciProVo;
import top.infopub.model.mkt.DeciPro;
import top.infopub.model.mkt.DeciProAtt;
import top.infopub.model.mkt.DeciProCoop;
import top.infopub.model.mkt.DeciProQuote;
import top.infopub.model.mkt.DeciProSub;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class DeciProServiceImpl extends GenericServiceImpl<DeciPro, String> implements
		DeciProService {

	@Autowired
	private DeciProDao deciProDao;
	
	@Autowired
    private DeciProCoopDao deciProCoopDao;
    
    @Autowired
    private DeciProSubDao deciProSubDao;
    
    @Autowired
    private DeciProQuoteDao deciProQuoteDao;
    
    @Autowired
    private DeciProAttDao deciProAttDao;
    
    @Autowired
    private WsFlowProcessService wsFlowProcess;
	
	@Override
	public GenericDao<DeciPro, String> getDao() {
		return deciProDao;
	}

    @Override
    public Page<DeciProDomain> selectPageByDomainWhere(Page<DeciProDomain> page,
                                                       DeciProVo deciProVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeciProDomain>)deciProDao.selectByDomainWhere(deciProVo);
    }

    @Override
    public Page<DeciProDomain> selectMyCheckByDomainWhere(Page<DeciProDomain> page,
                                                          DeciProVo deciProVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeciProDomain>)deciProDao.selectMyCheckByDomainWhere(deciProVo);
    }

    @Override
    public DeciProVo selectById(String id) {
        return deciProDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDeciPro(DeciProVo deciPro, List<DeciProCoop> deciProCoops,
                           List<DeciProSub> deciProSubs, List<DeciProQuote> deciProQuotes,
                           List<DeciProAtt> deciProAtts) {
        //插入预立项记录
        deciProDao.insertById(deciPro);
        
        //批量插入联合体单位
        if(CollectionUtils.isNotEmpty(deciProCoops)){
            deciProCoopDao.insertByBatch(deciProCoops);
        }
        
        //批量插入子项
        if(CollectionUtils.isNotEmpty(deciProSubs)){
            deciProSubDao.insertByBatch(deciProSubs);
        }
        
        //批量插入投标报价
        if(CollectionUtils.isNotEmpty(deciProQuotes)){
            deciProQuoteDao.insertByBatch(deciProQuotes);
        }
        
        //批量插入附件
        if(CollectionUtils.isNotEmpty(deciProAtts)){
            deciProAttDao.insertByBatch(deciProAtts);
        }
        
        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.MKT_DECI_PRO;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, deciPro.getId(), deciPro.getProcessId(), null);
        
    }

    @Override
    public List<DeciPro> selectUsableDeciPro() {
        return deciProDao.selectUsableDeciPro();
    }

    @Override
    public DeciPro selectByProCode(String proCode) {
        return deciProDao.selectByProCode(proCode);
    }

    @Override
    public List<DeciPro> selectListByProCode(String proCode) {
        return deciProDao.selectListByProCode(proCode);
    }

}